package com.gimi.cloud.jimi.manage.controller.sys;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.utils.MyBeanUtils;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.dto.sys.SysAuthorityDTO;
import com.gimi.cloud.jimi.dto.sys.SysMenuDTO;
import com.gimi.cloud.jimi.manage.service.sys.AuthService;
import com.gimi.cloud.jimi.manage.service.sys.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shazhiwei on 2017/12/18.
 */
@RestController
@RequestMapping(value = "auth")
public class AuthController {
    private final AuthService authService;
    private final MenuService menuService;

    public AuthController(AuthService authService,MenuService menuService){
        this.authService = authService;
        this.menuService = menuService;
    }

//    @RequestMapping(value = "/list")
//    public void list(@RequestBody AuthListParams params, HttpServletResponse response) {
//        Response resp = new Response();
//        Map<String,Object> data = new HashMap<>();
//        List<SysMenuDTO> list = authService.getAuthTree(params);//getAuthList(params);
//        List<SysMenuDTO> allList = menuService.getMenuList();
//        allList = menuService.sortMenu(allList);
//        List<SysMenuDTO> authList = menuService.getMenuListByRoleId(params.getRoleId());
//        data.put("allList",allList);
//        data.put("authList",authList);
//        resp.setRes_data(data);
//
//
//        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
//        Util.sendResponse(JsonUtil.toJson(resp), response);
//    }
//
//    @RequestMapping(value = "/doAuth")
//    public void doAuth(@RequestBody DoAuthParams params, HttpServletResponse response) throws Exception {
//        Response resp = new Response();
//        authService.doAuth(params);//getAuthList(params);
//        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
//        Util.sendResponse(JsonUtil.toJson(resp), response);
//    }

    /**
     * 根据角色id获取角色权限信息
     * @param request
     * @param response
     * @param sysAuthorityDTO
     * @throws Exception
     */
    @RequestMapping("/list")
    public Response roleMenus(HttpServletRequest request, HttpServletResponse response, @RequestBody SysAuthorityDTO sysAuthorityDTO)throws  Exception{
        Response resp = new Response();
        SysMenuDTO sysMenuDTO = new SysMenuDTO();
        MyBeanUtils.copyPropertiesIgnoreNullAndEmptyStr(sysAuthorityDTO,sysMenuDTO);
        List<SysMenuDTO> menuList = menuService.getMenuByRoId(sysMenuDTO,sysAuthorityDTO.getRoId());
        List<Integer> checked = new ArrayList<>();
        for(SysMenuDTO menuDto:menuList){
            if(null != menuDto.getHasAuth() && 1 == menuDto.getHasAuth()){
                checked.add(menuDto.getMeId());
            }
        }
        Map<String,Object> resData = new HashMap<>();
        resData.put("menus",menuService.menuToTree(menuList));
        resData.put("checked",checked);
        resp.setRes_data(resData);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        return resp;
    }

    @RequestMapping("/doAuth")
    public Response doAuth(HttpServletRequest request,HttpServletResponse response,@RequestBody  SysAuthorityDTO sysAuthorityDTO) throws  Exception{
        Response resp = new Response();
        authService.doAuth(sysAuthorityDTO,sysAuthorityDTO.getRoId(),sysAuthorityDTO.getMeIds());
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED,"授权成功");
        return resp;
    }
}
